package tasks.OpportunitiesBaseData.MainView;
import java.sql.SQLException;
import java.util.ArrayList;

import resources.tasks.OpportunitiesBaseData.MainView.STGVerifierAfterAppliedFiltersHelper;
import com.rational.test.ft.*;
import com.rational.test.ft.object.interfaces.*;
import com.rational.test.ft.object.interfaces.SAP.*;
import com.rational.test.ft.object.interfaces.WPF.*;
import com.rational.test.ft.object.interfaces.dojo.*;
import com.rational.test.ft.object.interfaces.siebel.*;
import com.rational.test.ft.object.interfaces.flex.*;
import com.rational.test.ft.object.interfaces.generichtmlsubdomain.*;
import com.rational.test.ft.script.*;
import com.rational.test.ft.value.*;
import com.rational.test.ft.vp.*;
import com.ibm.rational.test.ft.object.interfaces.sapwebportal.*;
/**
 * Description   : Functional Test Script
 * @author dimitar.vasilev
 */
public class STGVerifierAfterAppliedFilters extends STGVerifierAfterAppliedFiltersHelper
{
	/**
	 * Script Name   : <b>STGVerifierAfterAppliedFilters</b>
	 * Generated     : <b>Dec 14, 2011 1:04:47 PM</b>
	 * Description   : Functional Test Script
	 * Original Host : WinNT Version 6.1  Build 7601 (S)
	 * 
	 * @since  2011/12/14
	 * @author dimitar.vasilev
	 */
	public boolean verify(Filters filter)
	{
		boolean result = true;
		String taskReport = "";
			
		STGFrontEndWrapperAfterAppliedFilters frontEndWrapper = new STGFrontEndWrapperAfterAppliedFilters();
		STGDBWrapperAfterAppliedFilters dbContentWrapper = new STGDBWrapperAfterAppliedFilters();
		
		ArrayList<String> frontEndDetlKeys = null; 
		ArrayList<String> dbDetlKeys = null;


		boolean errorFlag = true;
		String feElementWrapper = "";
					 
		try {
			frontEndDetlKeys = frontEndWrapper.get();
			dbDetlKeys = dbContentWrapper.get(filter);
			
			if ((frontEndDetlKeys.size() == 0) && (dbDetlKeys.size()== 0)) 
			{
				setErrorComment("There are no records in database and front-end meeting selected filter criteria.");
			}
			else
			{
				if(frontEndDetlKeys.size() != dbDetlKeys.size())
				{
					setErrorComment("The records in the front end (" + frontEndDetlKeys.size() + ") have different count than the records in the database (" + dbDetlKeys.size() + ")");
					result = false;
				}
				
				for(String dbElement:dbDetlKeys)
				{
					errorFlag = true;
					
					for(String feElement:frontEndDetlKeys)
					{
						feElementWrapper = feElement;
						if(dbElement == null)
						{
							dbElement = "";
						}
						
						if(feElement == null)
						{
							feElement = "";
						}
						
						if(dbElement.trim().equals(feElement.trim()))
						{
							errorFlag = false;
							break;
						}
						errorFlag = true;
					}
					
					if(errorFlag)
					{
						setErrorComment("The DB value: \"" + dbElement + "\" CAN NOT be found in the front end.");
						result = false;
					}
					else
					{
						frontEndDetlKeys.remove(feElementWrapper);
					}
				}
					
				if(!frontEndDetlKeys.isEmpty())
				{
					for(String frontEndElement:frontEndDetlKeys)
					{
						setErrorComment("The Front End record with Detail key: \"" + frontEndElement + "\" CAN NOT be found in the database!");
					}
				}
			}		
		} catch (Exception e) {
			result = false;
			e.printStackTrace();
		}

		setTaskResult("Task: Verification of Front End and Database values.", result, taskReport);
		
		return result;
	}
}

